﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EntityBinder.Interface;

namespace EntityBinder.Internal
{
	/// <summary>
	/// This class provide access for Property in class.
	/// </summary>
	internal class Property : IMemberAccess
	{
		/// <summary>
		/// Property name.
		/// </summary>
		private string m_propertyName;

		/// <summary>
		/// Type of the container.
		/// </summary>
		private Type m_type;

		/// <summary>
		/// PropertyInfo relative to the Property.
		/// </summary>
		private System.Reflection.PropertyInfo m_propertyInfo;

		public string Name
		{
			get { return m_propertyName; }
		}

		public Type Type
		{
			get { return m_type; }
		}

		/// <summary>
		/// Create a Property object by specifying the name of the property which it will apply and the type of the container.
		/// </summary>
		/// <param name="_name">Name of the property.</param>
		/// <param name="_type">Type of the container of the property.</param>
		public Property(string _name, Type _type)
		{
			m_propertyName = _name;
			m_type = _type;

			m_propertyInfo = m_type.GetProperty(m_propertyName, MemberAccessFactory.SEARCH_FLAGS);

			if(m_propertyInfo == null)
			{
				throw new EntityBinderException("Property", "<ctor>", "The property " + _name + " does not exists in the type " + _type);
			}
		}

		public virtual object Get(object _data)
		{
			return m_propertyInfo.GetValue(_data, null);
		}

		public virtual void Set(object _data, object _newValue)
		{
			m_propertyInfo.SetValue(_data, _newValue, null);
		}
	}
}
